﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;

namespace ERTesting.Gui.Converter
{
    class NullToVisibilityConverter : DependencyObject, IValueConverter
    {

        /// <summary>
        /// The <see cref="TrueState" /> dependency property's name.
        /// </summary>
        public const string TrueStatePropertyName = "TrueState";

        /// <summary>
        /// Gets or sets the value of the <see cref="TrueState" />
        /// property. This is a dependency property.
        /// </summary>
        public Visibility TrueState
        {
            get
            {
                return (Visibility)GetValue(TrueStateProperty);
            }
            set
            {
                SetValue(TrueStateProperty, value);
            }
        }

        /// <summary>
        /// Identifies the <see cref="TrueState" /> dependency property.
        /// </summary>
        public static readonly DependencyProperty TrueStateProperty = DependencyProperty.Register(
            TrueStatePropertyName,
            typeof(Visibility),
            typeof(NullToVisibilityConverter),
            new UIPropertyMetadata(Visibility.Collapsed));

        /// <summary>
        /// The <see cref="FalseState" /> dependency property's name.
        /// </summary>
        public const string FalseStatePropertyName = "FalseState";

        /// <summary>
        /// Gets or sets the value of the <see cref="FalseState" />
        /// property. This is a dependency property.
        /// </summary>
        public Visibility FalseState
        {
            get
            {
                return (Visibility)GetValue(FalseStateProperty);
            }
            set
            {
                SetValue(FalseStateProperty, value);
            }
        }

        /// <summary>
        /// Identifies the <see cref="FalseState" /> dependency property.
        /// </summary>
        public static readonly DependencyProperty FalseStateProperty = DependencyProperty.Register(
            FalseStatePropertyName,
            typeof(Visibility),
            typeof(NullToVisibilityConverter),
            new UIPropertyMetadata(Visibility.Visible));


        #region Implementation of IValueConverter

        /// <summary>
        /// Converts a value. 
        /// </summary>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        /// <param name="value">The value produced by the binding source.</param><param name="targetType">The type of the binding target property.</param><param name="parameter">The converter parameter to use.</param><param name="culture">The culture to use in the converter.</param>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(typeof(Visibility) != targetType)
            {
                throw new ApplicationException("This converter can only convert to Visibility.");
            }

            return value == null ? TrueState : FalseState;
        }

        /// <summary>
        /// Converts a value. 
        /// </summary>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        /// <param name="value">The value that is produced by the binding target.</param><param name="targetType">The type to convert to.</param><param name="parameter">The converter parameter to use.</param><param name="culture">The culture to use in the converter.</param>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new ApplicationException("This conveter should be used on One way bindings only.");
        }

        #endregion
    }
}
